<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>民宿管理系统</title>
    <link rel="stylesheet" href="/static/common/layui/css/layui.css">
    <link rel="stylesheet" href="/static/admin/css/login.css">
    <script src="/static/common/layui/layui.js"></script>
    <script src="/static/common/jquery-3.3.1.min.js"></script>
    <script src="/static/common/vue.min.js"></script>
</head>
<body id="login">
<div class="login">
    <h2><strong><font face="仿宋">管理员账号注册</font></strong></h2>
<%--    <form class="layui-form" method="post" action="/admins">--%>
<%--        &lt;%&ndash;设置隐藏域&ndash;%&gt;--%>
<%--        <input type="hidden" name="action" value="register">--%>

        <div class="layui-form-item">
            <input type="text" name="username" id="username" value="${requestScope.data.username}" placeholder="用户名"
                   class="layui-input" required>
            <i class="layui-icon input-icon">&#xe66f;</i>
        </div>
        <div class="layui-form-item">
            <input type="password" name="password" id="password" value="${requestScope.data.password}" placeholder="密码"
                   class="layui-input" required>
            <i class="layui-icon input-icon">&#xe673;</i>
        </div>
        <div class="layui-form-item">
            <input type="password" name="password" id="againPassword" value="${requestScope.data.againPassword}" placeholder="确认密码"
                   class="layui-input" required>
            <i class="layui-icon input-icon">&#xe673;</i>
        </div>
        <div class="layui-form-item">
            <input type="text" name="usercode" id="usercode" placeholder="验证码" value="${requestScope.data.usercode}" style="width: 100px"
                   class="layui-input" required>
            <img src="/CodeServlet.do" style="width: 80px;position: absolute;top: 2px; left:115px"
                 onclick="changeCode(this);">
            <i class="layui-icon input-icon">&#xe673;</i>
        </div>
        <div class="layui-form-item">
            <button style="width: 100%;background-color: #36e136" class="layui-btn layui-btn-radius layui-btn-normal"
                    lay-submit lay-filter="register" onclick="register();">立即注册
            </button>
        </div>
        <div class="layui-form-item">
            <button style="width: 100%;" class="layui-btn layui-btn-radius layui-btn-normal" lay-submit
                    lay-filter="login" onclick="window.history.back();">返回登录
            </button>
        </div>
        <br>
<%--    </form>--%>
</div>
</body>
<script src="/static/admin/js/config.js"></script>
<script src="/static/admin/js/script.js"></script>
<script>
    function changeCode(img) {
        img.src = "/CodeServlet.do?time=" + new Date();
    }

    function register() {
        let username = $("#username").val();
        let password = $("#password").val();
        let againPassword = $("#againPassword").val();
        let usercode = $("#usercode").val();
        if (username === "" || password === "" || againPassword === "" || usercode === "") {
            alert("请填写完整注册信息");
            return;
        }
        $.ajax({
            url: "/admins",
            type: "POST",
            dataType: "json",
            data: {
                action: "register",
                username: username,
                password: password,
                againPassword: againPassword,
                usercode: usercode
            },
            success: function (data) {
                alert(data.msg);
                if (data.redirect) {
                    window.location.href = data.redirect; // 根据返回的地址跳转
                }
            }
        });
    }
</script>
</html>
